package com.iss.apartmentmanage.controller.room;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.iss.apartmentmanage.config.uploadpicture.PrereadConfig;
import com.iss.apartmentmanage.entity.RoomManage;
import com.iss.apartmentmanage.model.MyResponseData;
import com.iss.apartmentmanage.model.BaseModel;
import com.iss.apartmentmanage.model.GetForArrays;
import com.iss.apartmentmanage.service.impl.HotelAreaServiceImpl;
import com.iss.apartmentmanage.service.impl.RoomManagerServiceImpl;
import com.iss.apartmentmanage.utils.MyJson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RestController
@CrossOrigin
@Api(description = "房间管理模块接口",value = "RoomManagerController")
@RequestMapping("/roommanager")
public class RoomManagerController {
    @Autowired
    RoomManagerServiceImpl roomService;
    @Autowired
    HotelAreaServiceImpl hotelAreaService;
    @Autowired
    private PrereadConfig config;


    /**
     * 分页查询全部房间信息
     * @param pageNum
     * @param pageSize
     * @param baseModel
     * @return
     */
    @PostMapping("/selectAllRoom")
    public String selectAllRoom(@RequestParam int pageNum, @RequestParam int pageSize,
                                @RequestBody BaseModel baseModel) {
        MyResponseData myResponseData = new MyResponseData();
        RoomManage roomManage = new RoomManage();

        roomManage.setRoomName(baseModel.getRoomName());
        roomManage.setAreaId(baseModel.getAreaId());
        IPage<RoomManage> res = roomService.selectAllRoom(pageNum, pageSize,roomManage, baseModel);
        if (res!=null){
            myResponseData.setErrorCode(1);
            myResponseData.setMsg("成功");
            myResponseData.setObject(res);
        }

        return MyJson.getJson(myResponseData);
    }

    /**
     * 查询全部的空房间
     * @return
     */
    @PostMapping("/selectAllFreeRoom")
    public MyResponseData selectAllFreeRoom(){
        MyResponseData myResponseData = new MyResponseData();
        List<RoomManage> roomManages = roomService.selectAllEmptyRoom();
        if(roomManages!=null){
            myResponseData.setObjectList(roomManages);
            myResponseData.setErrorCode(1);
            myResponseData.setMsg("查询成功");
        }
        return myResponseData;
    }

    /**
     * 更新房间信息
     * @param roomManage
     * @return
     */
    @PostMapping("/updateRoomByRoomId")
    public MyResponseData updateRoomByRoomId(@RequestBody RoomManage roomManage ){
        MyResponseData myResponseData = new MyResponseData();
        int count= roomService.updateRoomByRoomId(roomManage);
        if (count!=0){
            myResponseData.setErrorCode(1);
            myResponseData.setMsg("成功");

        }
        return myResponseData;
    }

    /**
     * 批量更新房间isUsed禁用启用状态
     * @param getForArrays
     * @return
     */
    @PostMapping("/updateIsUsedByRoomIds")
    public MyResponseData updateIsUsedByRoomIds(@RequestBody GetForArrays getForArrays){
        MyResponseData myResponseData = new MyResponseData();
        int count= roomService.updateIsUsedByRoomIds(getForArrays.getId(), getForArrays.getIsUsed());
        if (count!=0){
            myResponseData.setErrorCode(1);
            myResponseData.setMsg("成功");

        }
        return myResponseData;
    }

    /**
     *  删除房间信息
     * @param id
     * @return
     */
    @PostMapping("/deleteRoomByid")
    public MyResponseData deleteRoomByid(@RequestParam String id){
        MyResponseData myResponseData = new MyResponseData();
        int count =roomService.deleteRoomByid(id);
        if (count!=0){
            myResponseData.setErrorCode(1);
            myResponseData.setMsg("成功");

        }else{
            myResponseData.setErrorCode(0);
            myResponseData.setMsg("失败");
        }
        return myResponseData;
    }

    /**
     * 批量删除房间信息
     * @param getForArrays
     * @return
     */
    @ApiOperation(value = "房间管理--批量删除房间信息")
    @PostMapping("/deleteRoomByBatchIds")
    public MyResponseData deleteRoomByBatchIds(@RequestBody GetForArrays getForArrays){
        MyResponseData myResponseData = new MyResponseData();
        int count =roomService.deleteRoomByBatchIds(getForArrays.getId());
        if (count!=0){
            myResponseData.setErrorCode(1);
            myResponseData.setMsg("成功");

        }
        return myResponseData;
    }

    /**
     * 查询全部区域-下拉框
     * @return
     */
    @PostMapping("/selectHotelArea")
    public MyResponseData selectHotelArea(){
        MyResponseData myResponseData = new MyResponseData();
        return null;
    }  //插入新的房间
    @PostMapping("/insertRoom")
    public MyResponseData insertRoom(@RequestBody RoomManage roomManage){
        MyResponseData myResponseData = new MyResponseData();
        Integer count = roomService.insertRoom(roomManage);
        if(count>0){
            myResponseData.setErrorCode(1);
            myResponseData.setStatus("插入数据成功");
        }
        return myResponseData;
    }

    //查看房间图片
    @RequestMapping("/selectpicture")
    public MyResponseData selectpicture(Integer  em_id){
        System.out.println("em_id="+em_id);
        MyResponseData responseData = new MyResponseData();
       // String em_picture= service.selectpicture(em_id);
//        System.out.println("em_picture="+em_picture);
//        responseData.setStatus(em_picture);
        return responseData;
    }


    //上传下载图片
    @RequestMapping("/upload")
    public  MyResponseData upload(@RequestParam(value = "files", required = false) MultipartFile[] files, Integer roomId){
        MyResponseData responseData = new MyResponseData();
        RoomManage roomManage = new RoomManage();
        System.out.println(roomId);
        System.out.println(files);
        if (files == null || files.length == 0) {
            responseData.setErrorCode(-1);
            responseData.setStatus("没有选择文件");
        }

        File path = new File(config.getUploadPath()+File.separator+roomId);
        if (!path.exists()) {
            path.mkdirs();
        }

        String surfaceName = "";
        for (int i = 0; i < files.length; i++) {
            String newFileName = System.currentTimeMillis() + "_" + files[i].getOriginalFilename();
            File newFile = new File(path, newFileName);   //保存文件
            if (i == 0) {
                surfaceName = "uploadFile"+File.separator+roomId+File.separator+newFileName;
            }
            try {
                FileOutputStream fos = new FileOutputStream(newFile);
                InputStream is = files[i].getInputStream();
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = is.read(buffer)) != -1) {
                    fos.write(buffer, 0, len);
                }
                fos.close();
                is.close();
                responseData.setErrorCode(0);
                responseData.setStatus("上传成功");
                roomManage.setRoomPicture(surfaceName);
                roomManage.setId(roomId);
                Integer count = roomService.updateRoomPicture(roomManage);
                responseData.setStatus(surfaceName);
            } catch (IOException e) {
                responseData.setErrorCode(-2);
                responseData.setStatus("上传失败");
                e.printStackTrace();
            }
        }
        return responseData;
    }

}
